#include <DS1307RTC.h>
#include <Wire.h>
#include <IRremote.h>
#include <NewSoftSerial.h>
#include <goldelox.h>
#include <alice.h>
#include <Time.h>
#include <DB.h>
#include <LSM303compass.h>
#include <MatrixMath.h>

LSM303compass c;
vector max_m,max_a,min_m,min_a;
int flag=0;

void setup()  {

Serial.begin(9600);

c.enable();

c.read();

max_m.x =c.m.x;max_m.y =c.m.y;max_m.z =c.m.z;
max_a.x =c.a.x;max_a.y =c.a.y;max_a.z =c.a.z;
min_m.x =c.m.x;min_m.y =c.m.y;min_m.z =c.m.z;
min_a.x =c.a.x;min_a.y =c.a.y;min_a.z =c.a.z;
print_max_min();
}

void loop(){
	c.read();
	if (c.m.x>max_m.x) { flag=1; max_m.x =c.m.x; }
	if (c.m.y>max_m.y) { flag=1; max_m.y =c.m.y; }
	if (c.m.z>max_m.z) { flag=1; max_m.z =c.m.z; }
	if (c.a.x>max_a.x) { flag=1; max_a.x =c.a.x; }
	if (c.a.y>max_a.y) { flag=1; max_a.y =c.a.y; }
	if (c.a.z>max_a.z) { flag=1; max_a.z =c.a.z; }
	if (c.m.x<min_m.x) { flag=1; min_m.x =c.m.x; }
	if (c.m.y<min_m.y) { flag=1; min_m.y =c.m.y; }
	if (c.m.z<min_m.z) { flag=1; min_m.z =c.m.z; }
	if (c.a.x<min_a.x) { flag=1; min_a.x =c.a.x; }
	if (c.a.y<min_a.y) { flag=1; min_a.y =c.a.y; }
	if (c.a.z<min_a.z) { flag=1; min_a.z =c.a.z; }

        if ( flag==1) {
          print_max_min();
          flag=0;
        }  
}

void print_max_min()
{
  Serial.print(max_a.x);
  Serial.print(" ");  
  Serial.print(max_a.y);
  Serial.print(" ");    
  Serial.print(max_a.z);
  Serial.print(" ");    
  Serial.print(min_a.x);
  Serial.print(" ");    
  Serial.print(min_a.y);
  Serial.print(" ");    
  Serial.print(min_a.z);
  Serial.print(" ");    

  Serial.print(max_m.x);
  Serial.print(" ");  
  Serial.print(max_m.y);
  Serial.print(" ");    
  Serial.print(max_m.z);
  Serial.print(" ");    
  Serial.print(min_m.x);
  Serial.print(" ");    
  Serial.print(min_m.y);
  Serial.print(" ");    
  Serial.println(min_m.z);

}

void print_readings()
{
  Serial.print(c.a.x);
  Serial.print(c.a.y);
  Serial.print(c.a.z);
  Serial.print(c.m.x);
  Serial.print(c.m.y);
  Serial.println(c.m.z);
}
